﻿using System.Text;
using System.Net;
using System.IO;
using System.Web;


namespace SZJT.Framework.Adapter
{
    internal class HttpProxy 
    {
        private string PageUrl { get; set; }
        public HttpProxy(string url)
        {
            PageUrl = url;
        }

        internal string BuildGetParam(string userId, string taskGuid, string dataGuid, string dataType)
        {
            return "UserID=" + userId
                + "&TaskGuid=" + HttpUtility.UrlEncode(taskGuid)
                + "&DataGuid=" + HttpUtility.UrlEncode(dataGuid)
                + "&DataType=" + HttpUtility.UrlEncode(dataType);
        }

        internal string BuildSetParam(string userId, string taskGuid, string dataGuid, string dataType, string xmlData)
        {
            return "UserID=" + HttpUtility.UrlEncode(userId)
                + "&TaskGuid=" + HttpUtility.UrlEncode(taskGuid)
                + "&DataGuid=" + HttpUtility.UrlEncode(dataGuid)
                + "&DataType=" + HttpUtility.UrlEncode(dataType)
                + "&XmlData=" + HttpUtility.UrlEncode(xmlData);
        }

        internal string BuildTransformParam(string userId, string taskGuid, string xmlParam)
        {
            return "UserID=" + HttpUtility.UrlEncode(userId)
                + "&TaskGuid=" + HttpUtility.UrlEncode(taskGuid)
                + "&XmlTransfrom=" + HttpUtility.UrlEncode(xmlParam);
        }
        private string Post(string url,string param)
        {
            var strRespone = string.Empty;
            HttpWebRequest request;
            HttpWebResponse response = null;
            try
            {
                // 将提交的字符串数据转换成字节数组 
                var postData = Encoding.UTF8.GetBytes(param);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.KeepAlive = false;
                request.AllowAutoRedirect = true;
                request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                request.ContentLength = postData.Length;

                // 提交请求数据 
                using (var outputStream = request.GetRequestStream())
                {
                    outputStream.Write(postData, 0, postData.Length);
                    response = request.GetResponse() as HttpWebResponse;
                    using (var _responseStream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(_responseStream, Encoding.UTF8))
                        {
                            strRespone = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch { }
            finally 
            {
                if (response != null)
                    response.Close();
            }
            return strRespone;
        }

        
        internal string Get(string url, string param)
        {
            var strRespone = string.Empty;
            HttpWebResponse response = null;
            StreamReader responseStream = null;
            var requestUrl = url + "?" + param;
            var request = WebRequest.Create(requestUrl) as HttpWebRequest;
            request.Method = "GET";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                responseStream = new StreamReader(response.GetResponseStream());
                strRespone = responseStream.ReadToEnd();
            }
            catch
            {
            }
            finally
            {
                if (response != null)
                    response.Close();
                if (responseStream != null)
                    responseStream.Close();
            }
            return strRespone;
        }
      
        internal string GetResult(string param,string type="POST")
        {
            return type.ToUpper().Equals("GET") ? Get(PageUrl, param) : Post(PageUrl, param);
        }
    }
}
